#include "siyicameracontrol.h"
siyi::Camera siyi_camera;
SiYiCameraControl::SiYiCameraControl(QObject *parent)
    : QObject{parent}, m_init_flag(false),
                       m_ip("192.168.1.64")
{

}
SiYiCameraControl::~SiYiCameraControl(){

}
void SiYiCameraControl::SiYiCameraInit(){
    // init
    siyi_camera.connect_camera(m_ip, 37260);
    while (!siyi_camera.init()) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        std::cout << "connecting ...\n";
        continue;
    }
    m_init_flag = true;

    auto curr_attitude = siyi_camera.get_current_attitude();
    m_curr_yaw = curr_attitude.first;
    m_curr_pitch = curr_attitude.second;
}

void SiYiCameraControl::handleUp(){
    if (!m_init_flag) return;
    m_curr_pitch -= 5.0;
    if (m_curr_pitch <= -90.0) m_curr_pitch = -90.0;

    siyi_camera.controlCamera(m_curr_yaw, m_curr_pitch);
}
void SiYiCameraControl:: handleDown(){
    if (!m_init_flag) return;
    m_curr_pitch += 5.0;
    if (m_curr_pitch >= 25.0) m_curr_pitch = 25.0;

    siyi_camera.controlCamera(m_curr_yaw, m_curr_pitch);
}
void SiYiCameraControl::handleLeft(){
    if (!m_init_flag) return;
    m_curr_yaw -= 5.0;
    if (m_curr_yaw <= -270) m_curr_yaw = -270.0;

    siyi_camera.controlCamera(m_curr_yaw, m_curr_pitch);
}
void SiYiCameraControl::handleRight(){
    if (!m_init_flag) return;
    m_curr_yaw += 5.0;
    if (m_curr_yaw >= 270) m_curr_yaw = 270.0;

    siyi_camera.controlCamera(m_curr_yaw, m_curr_pitch);
}

void SiYiCameraControl::handleReset(){
    if (!m_init_flag) return;
    m_curr_pitch = 0.;
    m_curr_yaw = 0.;

    siyi_camera.controlCamera(m_curr_yaw, m_curr_pitch);
}
